import 'package:cateapp/base_quest.dart';
import 'package:cateapp/food_request.dart';
import 'package:cateapp/generated/json/base/json_convert_content.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'base_url.dart';
import 'generated/json/base/doc_home_list_news_entity.dart';
import 'generated/json/base/doctor_team_list_entity.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  List<DocHomeListNewsDataRecommand>  homeListArray = [];

  var docListArray = <DoctorTeamListData>[];

  @override
  void initState() {
    // TODO: implement initState
  
    print("开始执行");
//    1.单个请求
//    getTitle();

//    1.2 利用stream  进行异步单个请求
//    Stream.fromFuture(getTitle()).listen((event) {
//        print("stream-event");
//    }).onDone(() {
//      print("stream-onDone");
//    });

//    2 多个请求，等待数据 ，回调刷新UI
    Future.wait([getHomeListNews(),getDocTeamList()]).then((_) {
      print("接收数据");
      setState(() {

      });
    }).whenComplete(() {
      print("执行完毕");
    });

//    2.1 利用stream 多个请求
//    Stream.fromFutures([getTitle(),getContent()]).listen((event) {
//      print("streamMult-event-$event");
//    }).onDone(() {
//      print("streamMult-onDone");
//    });
//      getHomeListNews();
//      getDocTeamList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {


  return MaterialApp(
    debugShowCheckedModeBanner: false,
    home: Scaffold(
      appBar: AppBar(
        title: Text("美食广场"),
      ),
      body:ListView.builder(itemBuilder: (ctx,key){

        return ListTile(

          title: Text("${homeListArray[key].title}"),
          subtitle: Text(docListArray[key].teamName),
        );
      },
        itemCount: homeListArray.length,
      )
    ),
  );
  }


  Future getHomeListNews() async{
    print("首页news");
    await FootRequest.getHomeListNewsRequestBlock(null, (t){
//     for (var newModel in t.data.recommend) {
//       print(newModel.title);
//       print(newModel.unitName);
//     }
     print("首页news输出完毕");
//     setState(() {
       homeListArray = t.data.recommend;
//     });
    },onError: (error) {

    }
    );
}

  Future getDocTeamList() async{
    print("医生团队");
    await FootRequest.getDoctorTeamListRequestBlock(null, (t){
//      for (var newModel in t.data) {
//        print(newModel.teamName);
//      }
    docListArray = t.data;
      print("医生团队输出完毕");
    });
  }
//   Future getTitle() async{
//    print("标题");
//   Map a = {
//      "id_type": 2,
//     "client_type": 2608,
//     "sort_type": 200,
//     "cursor": "eyJ2IjoiNjkzOTc2NjQzNDE1OTM5NDgzMCIsImkiOjgwfQ==",
//     "limit": 20
//     };
//    await FootRequest.getFoodListRequestBlock(a, (data){
////      FoodModelEntity data = JsonConvert.fromJsonAsT(t);
////      List<FoodModelDataListTargetDataArticleInfo> titleArrr = [];
////      data.data.xList.forEach((element) {
////        if (element.targetData.articleInfo != null) {
////          print(element.targetData.articleInfo.title);
////          titleArrr.add(element.targetData.articleInfo);
////        }
//
//
//      }
//
//      );
//      setState(() {
////        tiitleArray = titleArrr;
//      });
//    },onError: (error){
//      print(error);
//    });
//  }
//  Future getContent() async{
//    print("内容");
//    await FootRequest.getFoodListRequestBlock({"user_id":"3139860942038334","cursor":"0"}, (t){
////      FoodModelEntity data = JsonConvert.fromJsonAsT(t);
////      data.data.xList.forEach((element) {
////        if (element.targetData.articleInfo != null) {
////          print(element.targetData.articleInfo.briefContent);
////        }
//      });
//    },onError: (error){
//      print(error);
//    });
//  }
}

